package com.comment.project.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author liupenghui
 * @date 2018/7/23
 * @desc：
 */
public class fileOutputStream {

    private static void writeFile(String path, String content){
        FileOutputStream fileOutputStream = null;

        // 1、根据文件路径创建输出流
        try {
            fileOutputStream = new FileOutputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("创建出入流失败");
        }

        // 2、将输出内容转换成byte数组
        byte[] bytes = content.getBytes();

        // 3、将数组输出
        try {
            fileOutputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写数据失败");
        } finally {
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("关闭输出流失败");
                }
            }
        }
    }

    public static void main(String[] args) {
        String path = "C:\\Users\\Administrator\\Desktop\\";
        writeFile(path+"test.txt",path+path);
    }
}
